MCU = atmega168
F_CPU = 16000000
UPLOAD_RATE = 19200
AVRDUDE_PROGRAMMER = stk500v1
FORMAT = ihex

WORKROOT = /var/tmp/portage/dev-embedded/arduino-0015/image
ARDUINO_INCDIR = $(WORKROOT)/usr/include/arduino
ARDUINO_LIBDIR = $(WORKROOT)/usr/lib/arduino

AVRDUDE = /usr/bin/avrdude
DTRPULSE = /usr/bin/dtrpulse
ETAGS = /usr/bin/etags

CC = avr-gcc
CC86 = gcc
OBJCOPY = avr-objcopy

OBJS = $(TARGET).o
CFLAGS = -mmcu=$(MCU) -Os -DF_CPU=$(F_CPU) -I../lib -std=gnu99
C86FLAGS = -std=gnu99 -DEWAVR_X86 -I../lib -I/usr/avr/include
CXXFLAGS = -mmcu=$(MCU) -Os -DF_CPU=$(F_CPU) -I../lib -std=gnu++98
CXX86FLAGS = -I../lib -std=gnu++98
OFLAGS = -j .text -j .data -O ihex

.SUFFIXES: .c .cpp .h .o .hex

all: tags $(TARGET).hex

x86: $(SOURCES) $(INCLUDES)
	$(CC86) $(C86FLAGS) $(SOURCES) -o $(TARGET).x86

$(TARGET).o: $(SOURCES) $(INCLUDES)
	$(CC) $(CFLAGS) $(SOURCES) -o $(TARGET).o

$(TARGET).hex: $(TARGET).o
	$(OBJCOPY) $(OFLAGS) $(TARGET).o $(TARGET).hex

upload: all
	$(DTRPULSE) /dev/ttyUSB0
	$(AVRDUDE) -F -p m168 -P /dev/ttyUSB0 -c stk500v1 -b 19200 -U flash:w:$(TARGET).hex:i

tags: $(SOURCES) $(INCLUDES)
	$(ETAGS) $(SOURCES) $(INCLUDES)

clean:
	rm -rf $(TARGET).o $(TARGET).hex $(TARGET).x86 TAGS *~
